package cn.iocoder.yudao.module.promotion.controller.admin.rankinglist;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.promotion.controller.admin.rankinglist.vo.RankingListDeatilRespVo;
import cn.iocoder.yudao.module.promotion.controller.admin.rankinglist.vo.RankingListPageReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.rankinglist.vo.RankingListRespVO;
import cn.iocoder.yudao.module.promotion.controller.admin.rankinglist.vo.RankingListSaveReqVO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.rankinglist.RankingListDO;
import cn.iocoder.yudao.module.promotion.service.rankinglist.RankingListService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 活动排行榜")
@RestController
@RequestMapping("/promotion/ranking-list")
@Validated
public class RankingListController {

    @Resource
    private RankingListService rankingListService;

    @PostMapping("/create")
    @Operation(summary = "创建活动排行榜")
//    @PreAuthorize("@ss.hasPermission('promotion:ranking-list:create')")
    public CommonResult<Long> createRankingList(@Valid @RequestBody RankingListSaveReqVO createReqVO) {
        return success(rankingListService.createRankingList(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新活动排行榜")
//    @PreAuthorize("@ss.hasPermission('promotion:ranking-list:update')")
    public CommonResult<Boolean> updateRankingList(@Valid @RequestBody RankingListRespVO updateReqVO) {
        rankingListService.updateRankingList(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除活动排行榜")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('promotion:ranking-list:delete')")
    public CommonResult<Boolean> deleteRankingList(@RequestParam("id") Long id) {
        rankingListService.deleteRankingList(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "根据Id获得活动排行榜详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('promotion:ranking-list:query')")
    public CommonResult<RankingListDeatilRespVo> getRankingList(@RequestParam("id") Long id) {
        return success(rankingListService.getRankingListDetailById(id));
    }

//    @GetMapping("/now")
//    @Operation(summary = "获得当前进行中的活动排行榜")
//    @PreAuthorize("@ss.hasPermission('promotion:ranking-list:query')")
//    public CommonResult<RankingListRespVO> getRankingListNow() {
//        LocalDateTime now = LocalDateTime.now();
//        RankingListDO rankingList = rankingListService.getRankingListNow(now);
//        return success(BeanUtils.toBean(rankingList, RankingListRespVO.class));
//    }

    @GetMapping("/page")
    @Operation(summary = "获得活动排行榜分页")
//    @PreAuthorize("@ss.hasPermission('promotion:ranking-list:query')")
    public CommonResult<PageResult<RankingListRespVO>> getRankingListPage(@Valid RankingListPageReqVO pageReqVO) {
        PageResult<RankingListDO> pageResult = rankingListService.getRankingListPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RankingListRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出活动排行榜 Excel")
//    @PreAuthorize("@ss.hasPermission('promotion:ranking-list:export')")
    @OperateLog(type = EXPORT)
    public void exportRankingListExcel(@Valid RankingListPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RankingListDO> list = rankingListService.getRankingListPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "活动排行榜.xls", "数据", RankingListRespVO.class,
                        BeanUtils.toBean(list, RankingListRespVO.class));
    }

}